#pragma region Copyright
/// <copyright>
/// Copyright (c) 2007 Ilia Shramko.
/// Released under the terms of the GNU General Public License.
/// </copyright>
#pragma endregion

#include "stdafx.h"

FilterGraphClassFactory::FilterGraphClassFactory()
{
	ATLTRACE_TS(atlTraceGeneral, TraceLevel::Info,
		"FilterGraphClassFactory::FilterGraphClassFactory() : [0x%08X]\r\n", this);
}

FilterGraphClassFactory::~FilterGraphClassFactory()
{
	ATLTRACE_TS(atlTraceGeneral, TraceLevel::Info,
		"FilterGraphClassFactory::~FilterGraphClassFactory([0x%08X])\r\n", this);
}

HRESULT FilterGraphClassFactory::Initialize(IClassFactory* filterGraphClassFactory)
{
	if(filterGraphClassFactory == NULL) return E_INVALIDARG;
	_filterGraphClassFactory = filterGraphClassFactory;
	return S_OK;
}

STDMETHODIMP FilterGraphClassFactory::CreateInstance(LPUNKNOWN pUnkOuter, REFIID riid, void** ppv)
{
	if(ppv == NULL) return E_POINTER;
    *ppv = NULL;

	if(_filterGraphClassFactory == NULL)
		return E_UNEXPECTED;

	HRESULT hr = _filterGraphClassFactory->CreateInstance(pUnkOuter, riid, ppv);

	ATLTRACE_TS(atlTraceGeneral, TraceLevel::Info,
		"FilterGraphClassFactory::CreateInstance() : [0x%08X] - 0x%08X\r\n", *ppv, hr);

	if(SUCCEEDED(hr))
	{
		// Load OSH Mixer quering its interface.
		IUnknown* unknown = static_cast<IUnknown*>(*ppv);
		CComPtr<IOSHMixerPlugin> mixerPlugin;
		unknown->QueryInterface(&mixerPlugin);

		if(Configuration::Current().IsRegisterGraph())
		{
			ATLTRACE_TS(atlTraceGeneral, TraceLevel::Info,
				"FilterGraphClassFactory::CreateInstance() : registering graph in ROT...\r\n");

			GraphBulderROTRegister rotRegister(false);
			HRESULT hr = rotRegister.Register(unknown);
			//ATLASSERT(SUCCEEDED(hr));
		}
	}

	return hr;
}

STDMETHODIMP FilterGraphClassFactory::LockServer(BOOL fLock)
{
	if(fLock)
		_pAtlModule->Lock();
	else
		_pAtlModule->Unlock();
	return S_OK;
}

